package org.odk.clinic.android.activities;

import org.odk.clinic.android.R;
import org.odk.clinic.android.database.SQLiteAdapter;
import org.odk.clinic.android.openmrs1.problem;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class ProblemActivity extends Activity implements OnItemSelectedListener{
	private Spinner spinName;
	private EditText desc;
	private SQLiteAdapter mDB;
	private Cursor mCursor;
	private problem pl;
	private Button btnsave,btncancel;
	protected void onCreate(Bundle ici){
		super.onCreate(ici);
		setContentView(R.layout.problem);
		String[] items = new String[]{"Allery","Problem"};
		spinName = (Spinner)this.findViewById(R.id.spinProblemname);
		spinName.setAdapter(new ArrayAdapter<String> (getApplicationContext(),android.R.layout.select_dialog_item,items));
		spinName.setOnItemSelectedListener(this);
		
		//
		mDB = new SQLiteAdapter(this);
		mDB.open();
		
		desc = (EditText)this.findViewById(R.id.problemDesc);
		btnsave = (Button)this.findViewById(R.id.btn_problemsave);
		btnsave.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int mid = ListPatientActivity2.mid;
				String patient = null;
				mCursor = mDB.getPerson(mid);
				if(mCursor.moveToFirst()){
					patient = mCursor.getString(mCursor.getColumnIndex(SQLiteAdapter.KEY_GIVENNAME));
					int patient_id = mDB.find_patien_id(patient);
					pl =  new problem();
					pl.setProblemName(spinName.getSelectedItem().toString());
					pl.setProblemDesc(desc.getText().toString());
					pl.setPersonId(patient_id);
					mDB.createProblem(pl);
					setResult(RESULT_OK);
					finish();
					
				}
				
				
			}
		});
		
	}
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}

}
